package com.tianqu.tms.www.proxymember.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.ProxyCarInfo;
import com.tianqu.tms.www.proxymember.viewhelper.ProxyMemberCarSearchInputViewHelper;

/**
 * @author zhangxueliang
 * 待办车辆（已缴费）结果controller
 */
public class ProxyMemberCarPaidResultController extends TmsController{
	private Logger logger = Logger
	.getLogger(ProxyMemberCarPaidResultController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		try{
			controller.begin();
			ProxyCarInfo proxyCarInfo = ProxyCarInfo.getInstance();
			ProxyMemberCarSearchInputViewHelper viewHelper = (ProxyMemberCarSearchInputViewHelper)
			uc.getAttribute(TmsConstants.VIEWHELP);
			proxyCarInfo = viewhelperToEntity(viewHelper,proxyCarInfo);
			TmsUserProfile profile = uc.getUserProfile();
			proxyCarInfo.setUpdateUserId(profile.getUniqueId());
			proxyCarInfo.updateProxyCarPaidBySerial(proxyCarInfo);
			controller.commit();
		}catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
	/**
	 * viewhelper - > entity
	 * @param viewHelper
	 * @param entity
	 * @return
	 * @throws TmsDaoException 
	 */
	private ProxyCarInfo viewhelperToEntity(ProxyMemberCarSearchInputViewHelper viewHelper,
			ProxyCarInfo entity) throws TmsDaoException{
	
		Codes code = Codes.getInstance();
	
		entity.setNewOwnerType(code.getItemCodeByItemIdAndItemValue(Codes.CAR_OWNER,viewHelper.getNewOwnerType()));
		entity.setTransferType(code.getItemCodeByItemIdAndItemValue(Codes.TRANSFER_TYPE,viewHelper.getTransferType()));
		entity.setGainType(code.getItemCodeByItemIdAndItemValue(Codes.GAIN_TYPE,viewHelper.getGainType()));
		
		entity.setCarMarketIn(viewHelper.getCarMarketIn());
		entity.setMoveOutCity(viewHelper.getMoveOutCity());
		entity.setMoveOutProvince(viewHelper.getMoveOutProvince());
		entity.setPrice(Integer.parseInt(viewHelper.getPrice()));
		entity.setTaxNo(viewHelper.getTaxNo());
		entity.setPaymentPeople(viewHelper.getPaymentPeople());
		entity.setNewAgentAddress(viewHelper.getNewAgentAddress());
		entity.setNewAgentName(viewHelper.getNewAgentName());
		entity.setNewAgentOwnerIdCardNo(viewHelper.getNewAgentOwnerIdCardNo());
		entity.setNewAgentPhone(viewHelper.getNewAgentPhone());
		entity.setNewOwnerAddress(viewHelper.getNewOwnerAddress());
		entity.setNewOwnerOrgNo(viewHelper.getNewOwnerOrgNo());
		entity.setNewOwnerPhone(viewHelper.getNewOwnerPhone());
		entity.setNewcarOwnerIdCardNo(viewHelper.getNewcarOwnerIdCardNo());
		entity.setZipCode(viewHelper.getZipCode());
		entity.setMailAddress(viewHelper.getMailAddress());
		entity.setSerial(viewHelper.getSerial());
		entity.setNewCarOwner(viewHelper.getNewCarOwner());
		return entity;
	}
}
